<?php

/**
 * MVM_MALL 网上商店系统  定单管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-15 $
 * $Id: order.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');

if ($action=='list') {
    require_once MVMMALL_ROOT.'./include/pager.class.php';
    $search_sql = false;
    if($amode == 'detail_search') {
        $ps_duse    ==1 && $duse_check = 'checked';
        $search_sql = $ps_duse == 1 ? "addtime >'0'" : " addtime >= '".time_format($start_time). "' and addtime <= '".time_format($end_time)."' ";
        $ordersn   && $search_sql .= "  AND ordersn = '".filtering($ordersn)."'";
        $ps_email  && $search_sql .= "  AND email = '".filtering($ps_email)."'";
        $ps_name   && $search_sql .= "  AND consignee = '".filtering($ps_name)."'";
        $status    && $search_sql .= "  AND status = '".filtering($status)."'";
    } else {
        $search_sql = $status ? "status = '".filtering($status)."'" : '';
    }
    $search_sql  && $search_sql = "where ".$search_sql;
    $total_count   = $db->counter("{$tablepre}order_info", "$search_sql");
    $page          = $page ? (int)$page:1;
	$list_num      = 10;
	$rowset        = new Pager($total_count,$list_num,$page);
	$from_record   = $rowset->_offset();
    $result        = $db->query("SELECT * FROM `{$tablepre}order_info` $search_sql ORDER BY uid DESC LIMIT $from_record, $list_num");
    while ($rt     = $db->fetch_array($result)) {
        $rt['addtime']     = date($mm_date_format,$rt['addtime']);
        $rt['status']      = $lang['order_status'][$rt['status']];
        $rt['order_type']  = $m_order_type_array[$rt['order_type']];
        $rt['edit']        = "admincp.php?module=$module&action=edit&uid=$rt[uid]";
		$rt['del']         = "admincp.php?module=$module&action=del&uid=$rt[uid]";
		$rt['url']         = "admincp.php?module=$module&action=edit&per=view&uid=$rt[uid]";
    	$order_rt[]        = $rt;
    }
    $order_menu = drop_menu($lang['order_status'],'status',$status,'onchange="location.href=\''."admincp.php?module=order&action=list&start_date=$start_date&ps_duse=$ps_duse&ordersn=$ordersn&ps_email=$ps_email&ps_name=$ps_name&page=$page&status=".'\' + (this.options[this.selectedIndex].value)"');
    $page_list = $rowset->link("admincp.php?module=order&action=list&start_date=$start_date&ps_duse=$ps_duse&ordersn=$ordersn&ps_email=$ps_email&ps_name=$ps_name&status=$status&page=");
    require_once template('order');
    footer();
    
} elseif ($action=='edit' && is_numeric($uid)) {
    if($setp==1 && $_POST) {
        //订单数据处理
        $cas_exes                   = floatval($cas_exes);
        $sh_exes                    = floatval($sh_exes);
        $order_row['address']       = $province.' '.$city.' '.$address;
        $order_info                 = $db->get_one("SELECT * FROM `{$tablepre}order_info` WHERE uid='$uid'");
        $order_row['cas_name']      = $_SESSION['cart']['pack_name'] ? $_SESSION['cart']['pack_name']: $order_info['cas_name'];
        $order_row['cas_exes']      = ($_SESSION['cart']['pack_price'] && $_SESSION['cart']['pack_price']== $cas_exes) ? $_SESSION['cart']['pack_price'] : $cas_exes;
        $order_row['sh_id']         = $_SESSION['cart']['ship_id'] ? $_SESSION['cart']['ship_id']: $order_info['sh_id'];
        $order_row['sh_name']       = $_SESSION['cart']['ship_name'] ? $_SESSION['cart']['ship_name']: $order_info['sh_name'];
        $order_row['sh_exes']       = ($_SESSION['cart']['ship_price'] && $_SESSION['cart']['ship_price']== $sh_exes) ? $_SESSION['cart']['ship_price'] : $sh_exes;
        $order_row                  = dhtmlchars($order_row);
        $order_row['delivery_code'] && $order_row['sendtime'] = $m_now_time;
        //用户表
        $user_member                = $db->get_one("SELECT `member_id`,`myinfo`,`uid` FROM {$tablepre}member_table WHERE member_id = '$order_info[username]'");
        //获得商品信息
        $result     = $db->query("SELECT a.*,b.goods_sale_price,b.goods_file1,b.uid AS goods_id,b.goods_kg,b.goods_point,b.goods_code,b.goods_mony FROM `{$tablepre}order_goods` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE a.order_id='$uid' ORDER BY a.uid ASC ");
        while ( $rt = $db->fetch_array($result)) {
            //判断是否打开库存管理，如果是退货，加入库存数量
            if($mm_stock_use =='1') {
                if($order_row['status']=='9' && ($order_info['status']>'1' && $order_info['status']<'8')){
                    $db->query("UPDATE `{$tablepre}goods_table` SET goods_stock  = goods_stock + $rt[buy_number] WHERE uid = '$rt[goods_id]'");
                }elseif (($mm_stock_method==1 || $mm_stock_method==2 || $mm_stock_method==3) && $order_row['status']!=$order_info['status'] && ($order_row['status']==2 || $order_row['status']==6 || $order_row['status']==7)){
                    $db->query("UPDATE `{$tablepre}goods_table` SET goods_stock  = goods_stock - $rt[buy_number] WHERE uid = '$rt[goods_id]'");
                }
             
            }
            $money  += $rt['buy_price']   * $rt['buy_number'];
            $goods_mony += $rt['goods_mony'] * $rt['buy_number'];
            $point += $rt['goods_point'] * $rt['buy_number'];
        }
        
        //订单价格
        $order_row['goods_amount'] = floatval($money);
        $order_row['invoice']  && $cess_price = floatval($order_row['goods_amount']*$mm_cess);
        $order_row['order_amount'] = $order_row['sh_exes'] + $order_row['cas_exes']+$order_row['goods_amount'] + $cess_price;
        ($order_row['discount'] < 0 || $order_row['discount'] > $order_row['order_amount']) && exit('discount price error');
        $order_row['order_amount'] = $order_row['order_amount']-floatval($order_row['discount']);
        
        //送积分,判断是否开通
        if($mm_point_use=='1' && $user_member!=false){
            if($order_row['status']==$mm_point_method  && $order_info['status']!=$mm_point_method ){
                //记录表
                $user_point = $db->get_one("SELECT `point_sess`,`point_category` FROM {$tablepre}point_table WHERE `point_category`='1' AND `point_sess` = '$order_info[ordersn]'");
                //加积分
                if ($user_point==false && $point>0) {
                $query = "INSERT INTO `{$tablepre}point_table` SET
                      point_id       = '$user_member[member_id]',
                      point_sess     = '$order_info[ordersn]',
                      point_add      = '$point',
                      point_reason   = '$order_info[ordersn]',
                      modify_id      = '$m_check_id',
                      modify_ip      = '$m_user_ip',
                      point_category = '1',
                      register_date  = '$m_now_time',
                      approval_date  = '$m_now_time' ";
                $db->query($query);
                //更新会员数据库积分
                $db->query("UPDATE `{$tablepre}member_table` SET member_point = member_point+'$point' WHERE uid = '$user_member[uid]'");
                }
            } elseif ($order_row['status']=='9' && ($order_info['status']>'1' && $order_info['status']<'8') && $point>0){
                //退货，扣掉积分
                $query = "INSERT INTO `{$tablepre}point_table` SET
                      point_id       = '$user_member[member_id]',
                      point_sess     = '$order_info[ordersn]',
                      point_add      = '-$point',
                      point_reason   = '$m_order_array[9]',
                      modify_id      = '$m_check_id',
                      modify_ip      = '$m_user_ip',
                      point_category = '1',
                      register_date  = '$m_now_time',
                      approval_date  = '$m_now_time' ";
                $db->query($query);
                //更新会员数据库积分
                $db->query("UPDATE `{$tablepre}member_table` SET member_point = member_point-'$point' WHERE uid = '$user_member[uid]'");
            } 
        }
        //end
        
        //判断是否开启营销推广
        if($mm_extend == '1'){
            $user_myfo = $db->fetch_array($db->query("SELECT `point_sess`,`point_category` FROM {$tablepre}point_table WHERE `point_category`='6' AND `point_sess` = '$order_info[ordersn]'"));//记录表
            if ($user_member['myinfo']!='0' && $user_myfo==false) {
                //记录
                $new_point_text = "$order_info[ordersn]";
                $query = " INSERT INTO `{$tablepre}point_table` SET
                      point_id       = '$user_member[myinfo]',
                      point_sess     = '$order_info[ordersn]',
                      point_add      = '$goods_mony',
                      point_reason   = '$new_point_text',
                      modify_id      = '$order_info[username]',
                      point_category = '6',
                      modify_ip      = '$m_user_ip',
                      register_date  = '$m_now_time'";
                $db->query($query);
            }
        }
        $where = "uid='$order_info[uid]'";
        $order_info['delivery_code'] && $order_row['sendtime'] = $m_now_time;//发货时间
        $db->update("{$tablepre}order_info",$order_row, $where);
        //更改支付记录
        $order_row['order_amount']!=$order_info['order_amount'] && $db->query("UPDATE  `{$tablepre}pay_log` SET order_amount='$order_row[order_amount]' WHERE order_sn ='$order_info[ordersn]'");
        
        //短信发送
        if($mm_sms_use == '1' && $mm_sms_receipt=='1' && $order_row['status']=='2' && $order_info['status']!='2' && $order_row['buy_mobile']!=''){
            //结算确认发送短信
            sms_send($order_row['buy_mobile'], "$mm_sms_message3");
        }
        if($mm_sms_use == '1' && $mm_sms_delivery=='1'&&$order_row['status']=='4'&&$order_info['status']!='4' && $order_row['mobile']!=''){
            //商品发送完成发送短信
            sms_send($order_row['mobile'], "$mm_sms_message4");
        }
       
         //发送邮件
         if ($mm_mail_receipt==1 && $order_row['status']=='2'&&$order_info['status']!='2'){
         	//汇款确认
         	include MVMMALL_ROOT.'./language/'.$mm_lang.'/lang_'.$mm_lang.'.php';
         	unset($order_info);
         	$order_info = $db->get_one("SELECT uid,email,ordersn,order_amount,pay_name FROM `{$tablepre}order_info` WHERE uid='$uid'");
         	$order_info['order_amount']  = currency($order_info['order_amount']);
         	include_once template('email_pay');
         	$mail_content = clean_html('red');
         	smtp_mail($order_info['email'],$lang['buy_payment'],$mail_content,html);
         	unset($mail_content);
         }
       //发送确认 
       elseif ($mm_mail_delivery==1&&$order_row['status']=='4'&&$order_info['status']!='4') {
           //定单信息
           include MVMMALL_ROOT.'./language/'.$mm_lang.'/lang_'.$mm_lang.'.php';
           $order_info = $db->get_one("SELECT * FROM `{$tablepre}order_info` WHERE uid = '$uid'");
           $sql        = "SELECT a.*,b.goods_sale_price,b.goods_file1,b.uid AS goods_id,b.goods_kg,b.goods_point,b.goods_code FROM `{$tablepre}order_goods` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE a.order_id='$uid' ORDER BY a.uid ASC ";
           $result     = $db->query($sql);
           while($rt   = $db->fetch_array($result)) {
               $total_price          += $rt['buy_price'] * $rt['buy_number'];
               $rt['total_point']    = $rt['goods_point'] * $rt['cart_num'];
               $total_kg             += $rt['goods_kg'] * $rt['buy_number'];//计算总量
               $rt['photo']          = $rt['goods_file1'];
               $rt['url']            = GetBaseUrl('product',$rt['cart_goods_id']);
               $rt['subject']        = cut_str($rt['goods_name'],24);
               $rt['title']          = $rt['goods_name'];
               $rt['price']          = currency($rt['buy_price']);
               $rt['point']          = $rt['goods_point'];
               $rt['att_value']      = $rt['goods_att'];
               unset($rt['goods_att']);
               foreach (unserialize($rt['att_value']) as $val){
                   $rt['goods_att'].=$val.'<br/>';
               }
               $rt['kg']            = $rt['goods_kg'];
               $goods[] =  $rt;
           }
           $cess_price                 = currency(floatval($total_price*$mm_cess));
           $order_info['sh_exes']      = currency($order_info['sh_exes']);
           $order_info['cas_exes']     = currency($order_info['cas_exes']);
           $order_info['sh_exes']      = currency($order_info['sh_exes']);
           $order_info['goods_amount'] = currency($order_info['goods_amount']);
           $order_info['order_amount'] = currency($order_info['order_amount']);
           $order_info['addtime']      = date($mm_time_format,$order_info['addtime']);
           $order_info['sendtime']     = date($mm_time_format,$order_info['sendtime']);
           //end
           $mail_subject = $lang['sendgoods'];
           include_once template('email_sendgoods');
           $mail_content =  clean_html('red');
           $mail_content =  str_replace('shopimg',MVMMALL_DIR.'/shopimg',$mail_content);
           smtp_mail($order_info['email'],$mail_subject,$mail_content,html);
           unset($mail_content);
       }
        
        admin_msg('success','admincp.php?module=order&action=list');
    } 
    //显示编辑
    else {
        $uid          = (int)$uid;
        $order_info   = $db->get_one("SELECT * FROM `{$tablepre}order_info` WHERE uid = '$uid'");
        $goods_rt     = order_goods($uid);
        $goods        = $goods_rt['goods'];
        $add_rt       = explode(' ',$order_info['address']);
        $sh_exes      = currency($order_info['sh_exes']);
        $cas_exes     = currency($order_info['cas_exes']);
        $sh_exes      = currency($order_info['sh_exes']);
        $goods_amount = currency($order_info['goods_amount']);
        $order_amount = currency($order_info['order_amount']);
        $discount     = currency($order_info['discount']);
        $cess_price   = $order_info['invoice']  ? currency(floatval($order_info['goods_amount']*$mm_cess)):currency(0.00);
        $order_menu   = drop_menu($lang['order_status'],'order_row[status]',$order_info['status']);
        $per=='view' ? require_once template('order_view') :require_once template('order_add');
        footer();
    }
} 
/**删除定单商品数据**/
elseif($action == 'delete' && is_numeric($goods_id) && is_numeric($uid) && is_numeric($order_id)) {
    $order = $db->get_one("SELECT sh_id,cas_exes,sh_exes,goods_amount,invoice,order_amount,ordersn,uid,status,username FROM `{$tablepre}order_info` WHERE uid='$order_id'");
    if($order['status']>='2' && $order['status']<'8' ){
        $arr = array('err'=>'采购完成不能修改');
    }else {
        $sql        = "SELECT a.*,b.uid AS goods_id,b.goods_point FROM `{$tablepre}order_goods` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE a.goods_id='$goods_id'";
        $goods_info = $db->get_one($sql);
        //如果退货，扣掉积分
        if($order['status']=='9'){
            $buyer_point = $order['buy_number']*$order['goods_point'];//积分
            if($buyer_point>0){
                $query = "UPDATE `{$tablepre}point_table` SET
                 point_add      =  point_add - '$buyer_point',
                 modify_id      = '$m_check_id',
                 modify_ip      = '$m_user_ip'
                 WHERE point_sess = '$goods_info[ordersn]' AND point_category = '1' ";
                $db->query($query);
                //更新会员数据
                $order['username'] && $db->query("UPDATE `{$tablepre}member_table` SET member_point = member_point-'$buyer_point' WHERE member_id = '$order[username]'");
            }

        }
        //判断是否打开库存管理，如果是退货，加入库存数量
        if($mm_stock_use =='1' && $order['status']=='9') {
            $db->query("UPDATE `{$tablepre}goods_table` SET goods_stock  = goods_stock + $goods_info[buy_number] WHERE uid = '$goods_id'");
        }
        //删除定单商品数据，并更新定单价格
        $buyer_price  = $goods_info['buy_price']*$goods_info['buy_number'];//商品价格
        $db->query("DELETE FROM `{$tablepre}order_goods` WHERE uid ='$uid'");
        $order['invoice'] && $cess_price = $buyer_price*$mm_cess;
        $db->query( "UPDATE `{$tablepre}order_info` SET goods_amount = goods_amount - '$buyer_price',order_amount = order_amount-'$buyer_price'-'$cess_price' WHERE uid = '$order_id'");
        //json数据
        $goods_rt     = order_goods($order_id);
        $cess_price   = $order['invoice'] ? $goods_rt['sum']['price']*$mm_cess :0.00;
        $sh_exes      = ship_price($order['sh_id'],$goods_rt['sum']['kg'],$goods_rt['sum']['price']);
        $order_amount = ($goods_rt['sum']['price']+$sh_exes+$cess_price)-$order['discount'];
        $db->query("UPDATE `{$tablepre}order_info` SET goods_amount='{$goods_rt['sum']['price']}',sh_exes='$sh_exes',order_amount='$order_amount' WHERE uid='$order_id'");
        $arr = array('cess_price'=>currency($cess_price),'goods_amount'=>currency($goods_rt['sum']['price']),'order_amount'=>currency($order_amount),'kg'=>$goods_rt['sum']['kg'],'nums'=>$goods_rt['sum']['nums'],'point'=>$goods_rt['sum']['point'],'sh_exes'=>$sh_exes,'err'=>'0');
    }
    require_once MVMMALL_ROOT.'./include/json.class.php';
    $json = new Services_JSON;
    echo $json->encode($arr);
    //admin_msg('success',dreferer());
} elseif ($action=='del' && is_numeric($uid)) {
    $rt = $db->get_one("SELECT ordersn,uid FROM `{$tablepre}order_info` WHERE uid='$uid'");
    $db->query("DELETE FROM `{$tablepre}pay_log` WHERE order_sn ='$rt[ordersn]'");
    $db->query("DELETE FROM `{$tablepre}order_info` WHERE uid='$uid'");
    $db->query("DELETE FROM `{$tablepre}order_goods` WHERE order_id ='$uid'");
} elseif ($action=='all_delete' && is_array($uid_check)){
    foreach ($uid_check as $val){
        $uid = (int)$val;
        $rt = $db->get_one("SELECT ordersn,uid FROM `{$tablepre}order_info` WHERE uid='$uid'");
        $db->query("DELETE FROM `{$tablepre}pay_log` WHERE order_sn ='$rt[ordersn]'");
        $db->query("DELETE FROM `{$tablepre}order_info` WHERE uid='$uid'");
        $db->query("DELETE FROM `{$tablepre}order_goods` WHERE order_id ='$uid'");
    }
    admin_msg('success',dreferer());
}
//合并订单
elseif ($action=='merge'){
    if($setp==1 && $_POST) {
       $order_one == $order_two && admin_msg('主从订单不可一样');
       $one_rt = $db->get_one("SELECT cas_exes,sh_exes,goods_amount,invoice,order_amount,ordersn,uid FROM `{$tablepre}order_info` WHERE ordersn='$order_one'");
       $two_rt = $db->get_one("SELECT goods_amount,ordersn,uid FROM `{$tablepre}order_info` WHERE ordersn='$order_two'");
       //更新主定单数据
       $order_amount = $two_rt['goods_amount'] + $one_rt['goods_amount']+$one_rt['cas_exes']+$one_rt['sh_exes'];
       $one_rt['invoice'] && $order_amount +=($order_amount*$mm_cess);
       $db->query("UPDATE `{$tablepre}order_info` SET goods_amount=goods_amount+$two_rt[goods_amount],order_amount='$order_amount' WHERE ordersn='$one_rt[ordersn]'");
       $db->query("UPDATE `{$tablepre}order_goods` SET order_id='$one_rt[uid]' WHERE order_id='$two_rt[uid]'");
       //删除次订单
       $db->query("DELETE FROM `{$tablepre}pay_log` WHERE order_sn ='$two_rt[ordersn]'");
       $db->query("DELETE FROM `{$tablepre}order_info` WHERE uid='$two_rt[uid]'");
       $db->query("DELETE FROM `{$tablepre}order_goods` WHERE order_id ='$two_rt[uid]'");
       admin_msg('success',dreferer());
    }else  {
        //定单菜单
        $order_rt = $db->get_all("SELECT ordersn,consignee FROM {$tablepre}order_info ");
        foreach ($order_rt as $val){
            $arr[0]               = '-------------';
            $arr[$val['ordersn']] = $val['ordersn'].'  '.$val['consignee'];
        }
        $one_menu = drop_menu($arr,'one_menu');
        $two_menu = drop_menu($arr,'two_menu');
        //end
        require_once template('merge');
        footer();
    }

}
//更新订单商品信息
elseif ($action=='up_goods'){
    $goods_id     = (int)$goods_id;
    $order_id     = (int)$order_id;
    $uid          = (int)$uid;
    $order_info   = $db->get_one("SELECT * FROM `{$tablepre}order_info` WHERE uid = '$order_id'");
    if ($per=='nums') {
        if($order_info['status']>='2' && $order_info['status']<'8' ){
            $arr = array('err'=>'采购完成不能修改');
        }else {
            //更改数量
            $goods_nums  = intval($goods_nums);
            if(goods_stock_check($goods_id,$goods_nums)){
                $db->query("UPDATE `{$tablepre}order_goods` SET buy_number='$goods_nums' WHERE uid='$uid'");
                $goods_rt     = order_goods($order_id);
                $cess_price   = $order_info['invoice'] ? $goods_rt['sum']['price']*$mm_cess :0.00;
                $sh_exes      = ship_price($order_info['sh_id'],$goods_rt['sum']['kg'],$goods_rt['sum']['price']);
                $order_amount = ($goods_rt['sum']['price']+$sh_exes+$cess_price)-$order_info['discount'];
                $db->query("UPDATE `{$tablepre}order_info` SET goods_amount='{$goods_rt['sum']['price']}',sh_exes='$sh_exes',order_amount='$order_amount' WHERE uid='$order_id'");
                $arr = array('cess_price'=>currency($cess_price),'goods_amount'=>currency($goods_rt['sum']['price']),'order_amount'=>currency($order_amount),'kg'=>$goods_rt['sum']['kg'],'nums'=>$goods_rt['sum']['nums'],'point'=>$goods_rt['sum']['point'],'sh_exes'=>$sh_exes,'err'=>'0');
            }else {
                $arr = array('err'=>'商品库存不足');
            }
        }
    } elseif ($per=='add'){
        //添加商品
        if ($setp==1) {
            if($order_info['status']>='2' && $order_info['status']<'8' ){
                $arr = array('err'=>'采购完成不能修改');
            }else{
                $row = array();
                if(is_array($att_value)){
                    $goods_att           = '';
                    foreach ($att_value as $key=>$val){
                        $val             = (int)$val;
                        $att_list        = $db->get_one("SELECT a.*,b.id,b.att_name FROM `{$tablepre}att_value` a LEFT JOIN `{$tablepre}attribute` b ON a.bute_id=b.id WHERE a.att_id='$val'");
                        $goods_att       .= "$att_list[att_name]:$att_list[att_value]($att_list[att_price])  ";
                    }
                    $goods_att           =  dhtmlchars($goods_att);
                }else {
                    $goods_att           = false;
                }

                $row['order_id']      = $order_id;
                $row['goods_id']      = $goods_id;
                $row['buy_number']    = (int)$buy_number;
                $row['buy_price']     = sprintf("%01.2f",floatval($add_price=='custom' ? $custom_price :$add_price));
                $row['goods_attr']    =  $goods_att;
                $row['register_date'] = $m_now_time;
                $insert_id = $db->insert("{$tablepre}order_goods",$row);
                $goods_rt = $db->get_one("SELECT goods_name,goods_code,goods_kg,goods_point,uid FROM `{$tablepre}goods_table` WHERE uid='$row[goods_id]'");
                //商品格式html
                $goods_html  = "<tr class=\"trout\" onMouseOver=\"this.className='trover'\" onMouseOut=\"this.className='trout'\" id=\"r$insert_id\">
			<td class=\"td1\"><a href=\"#\">$goods_rt[goods_name]</a><p class=\"cpsm\">编码: $goods_rt[goods_code]</p></td>
			<td>
			<textarea name=\"attr_$insert_id\" id=\"attr_$insert_id\" onchange=\"up_attr('$order_id','$goods_id','$insert_id');\" cols=\"15\" rows=\"1\" class=\"text\">$goods_att</textarea>
			</td>
			<td><input type=\"text\"  name=\"price_$insert_id\" id=\"price_$insert_id\" onchange=\"up_price('$order_id','$goods_id','$insert_id');\" value=\"$row[buy_price]\" size=\"10\" maxlength=\"10\" class=\"text\" /></td>
			<td><input type=\"text\"  name=\"nums_$insert_id\" id=\"nums_$insert_id\" onchange=\"up_nums('$order_id','$goods_id','$insert_id');\" value=\"$row[buy_number]\" size=\"5\" maxlength=\"10\" class=\"text\" /></td>
			<td>$goods_rt[goods_point]</td>
			<td>$goods_rt[goods_kg]</td>
			<td>
			<a href=\"javascript:;\" onclick=\"remove_goods('$insert_id','admincp.php?module=$module&action=delete&goods_id=$goods_id&uid=$insert_id&order_id=$order_id');\">$lang[remove]</a></td>
		    </tr>";
                $goods_rt     = order_goods($order_id);
                $cess_price   = $order_info['invoice'] ? $goods_rt['sum']['price']*$mm_cess :0.00;
                $sh_exes      = ship_price($order_info['sh_id'],$goods_rt['sum']['kg'],$goods_rt['sum']['price']);
                $order_amount = ($goods_rt['sum']['price']+$sh_exes+$cess_price)-$order_info['discount'];
                $db->query("UPDATE `{$tablepre}order_info` SET goods_amount='{$goods_rt['sum']['price']}',sh_exes='$sh_exes',order_amount='$order_amount' WHERE uid='$order_id'");
                $arr = array('cess_price'=>currency($cess_price),'goods_amount'=>currency($goods_rt['sum']['price']),'order_amount'=>currency($order_amount),'kg'=>$goods_rt['sum']['kg'],'nums'=>$goods_rt['sum']['nums'],'point'=>$goods_rt['sum']['point'],'sh_exes'=>$sh_exes,'goods_html'=>$goods_html,'err'=>'0');
            }
        } else {
            include template('add_order_goods');
            clean_html();
            exit;
        }
    }
    elseif ($per=='attr'){
        if($order_info['status']>='2' && $order_info['status']<'8' ){
            $arr = array('err'=>'采购完成不能修改');
        }else {
            $goods_attr = dhtmlchars($goods_attr);
            $db->query("UPDATE `{$tablepre}order_goods` SET goods_attr='$goods_attr' WHERE uid='$uid'");
            $arr = array('err'=>'0');
        }
    }
    else {
        if($order_info['status']>='2' && $order_info['status']<'8' ){
            $arr = array('err'=>'采购完成不能修改');
        }else {
            //更改价格
            $goods_price  = floatval($goods_price);
            $db->query("UPDATE `{$tablepre}order_goods` SET buy_price='$goods_price' WHERE uid='$uid'");
            $goods_rt     = order_goods($order_id);
            $cess_price   = $order_info['invoice'] ? $goods_rt['sum']['price']*$mm_cess :0.00;
            $order_amount = ($goods_rt['sum']['price']+$order_info['cas_exes']+$order_info['sh_exes']+$cess_price)-$order_info['discount'];
            $db->query("UPDATE `{$tablepre}order_info` SET goods_amount='{$goods_rt['sum']['price']}',order_amount='$order_amount' WHERE uid='$order_id'");
            $arr = array('cess_price'=>currency($cess_price),'goods_amount'=>currency($goods_rt['sum']['price']),'order_amount'=>currency($order_amount),'err'=>'0');
        }
    }
    require_once MVMMALL_ROOT.'./include/json.class.php';
    $json = new Services_JSON;
    echo $json->encode($arr);
}
else {
    admin_msg('pass_worng');
}

////配送费用
function ship_price($area_id,$total_kg,$total_price){
    global $db,$tablepre;
    //运费
    $area_id           = (int)$area_id;
    $total_kg          = floatval($total_kg);
    $total_price       = floatval($total_price);
    $area_rt           = $db->get_one("SELECT f.*,ff.* FROM `{$tablepre}ship_table` f LEFT JOIN `{$tablepre}area_table`  ff ON f.id=ff.ship_id WHERE ff.area_id = '$area_id'");
    if ($area_rt==false) {
        $rt_price = 0.00;
    }else {
        $area_rt['config'] = unserialize($area_rt['config']);
        require_once MVMMALL_ROOT.'./include/shipping/'.$area_rt['class_name'].'.class.php';
        $rowst_ar          = new $area_rt['class_name']($area_rt['config']);
        $rt_price          = $rowst_ar->exes($total_kg,$total_price);//配送费用
    }
    return floatval($rt_price);
}
